package com.ruoyi.userr.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.userr.mapper.UserFollowMapper;
import com.ruoyi.userr.domain.UserFollow;
import com.ruoyi.userr.service.IUserFollowService;

import javax.annotation.Resource;

/**
 * 用户关注关系Service业务层处理
 * 
 * @author sr
 * @date 2025-08-15
 */
@Service
public class UserFollowServiceImpl implements IUserFollowService 
{
    @Resource
    private UserFollowMapper userFollowMapper;

    /**
     * 查询用户关注关系
     * 
     * @param id 用户关注关系主键
     * @return 用户关注关系
     */
    @Override
    public UserFollow selectUserFollowById(Integer id)
    {
        return userFollowMapper.selectUserFollowById(id);
    }

    /**
     * 查询用户关注关系列表
     * 
     * @param userFollow 用户关注关系
     * @return 用户关注关系
     */
    @Override
    public List<UserFollow> selectUserFollowList(UserFollow userFollow)
    {
        return userFollowMapper.selectUserFollowList(userFollow);
    }

    /**
     * 新增用户关注关系
     * 
     * @param userFollow 用户关注关系
     * @return 结果
     */
    @Override
    public int insertUserFollow(UserFollow userFollow)
    {
        userFollow.setCreateTime(DateUtils.getNowDate());
        return userFollowMapper.insertUserFollow(userFollow);
    }

    /**
     * 修改用户关注关系
     * 
     * @param userFollow 用户关注关系
     * @return 结果
     */
    @Override
    public int updateUserFollow(UserFollow userFollow)
    {
        return userFollowMapper.updateUserFollow(userFollow);
    }

    /**
     * 批量删除用户关注关系
     * 
     * @param ids 需要删除的用户关注关系主键
     * @return 结果
     */
    @Override
    public int deleteUserFollowByIds(Integer[] ids)
    {
        return userFollowMapper.deleteUserFollowByIds(ids);
    }

    /**
     * 删除用户关注关系信息
     * 
     * @param id 用户关注关系主键
     * @return 结果
     */
    @Override
    public int deleteUserFollowById(Integer id)
    {
        return userFollowMapper.deleteUserFollowById(id);
    }

    @Override
    public List<UserFollow> getFollowedUsers(Long followerId) {
        return userFollowMapper.selectFollowedUsersByFollowerId(followerId);
    }

    @Override
    public Long getFollowCount(Long followerId) {
        // 容错处理：若followerId为null，直接返回0
        if (followerId == null) {
            return 0L;
        }
        // 调用Mapper统计，若查询结果为null（无数据），返回0
        Long count = userFollowMapper.countFollowedUsers(followerId);
        return count == null ? 0L : count;
    }
    @Override
    public Long getFanCount(Long followedId) {
        // 容错处理：若被关注者ID为null，返回0
        if (followedId == null) {
            return 0L;
        }
        // 调用Mapper统计，无数据时返回0
        Long count = userFollowMapper.countFollowers(followedId);
        return count == null ? 0L : count;
    }

    @Override
    public List<UserFollow> getFollowerList(Long followedId) {

        return userFollowMapper.selectFollowerList(followedId);
    }


}
